package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.mall.dto.LiveShopCartDTO;
import com.yang.mall.dto.LiveShopCartNumDTO;
import com.yang.mall.entity.LiveShopCart;
import com.yang.mall.entity.Product;
import com.yang.mall.mapper.LiveShopCartMapper;
import com.yang.mall.service.ILiveShopCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mall.service.IProductService;
import com.yang.mall.tools.BeanUtils;
import com.yang.mall.vo.LiveShopCartVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-25
 */
@Service
public class LiveShopCartServiceImpl extends ServiceImpl<LiveShopCartMapper, LiveShopCart> implements ILiveShopCartService {


    @Autowired
    private IProductService productService;
    /**
     * 购物车列表
     * @param liveShopCartDTO
     * @author:kadorlin
     * @date:2022/10/25 09:11
     * @return:com.baomidou.mybatisplus.core.metadata.IPage<com.yang.mall.vo.LiveShopCartVO>
     **/
    @Override
    public IPage<LiveShopCartVO> list(LiveShopCartDTO liveShopCartDTO) {
        Page<LiveShopCart> page = new Page<>(liveShopCartDTO.getPageNum(), liveShopCartDTO.getPageSize());
        QueryWrapper<LiveShopCart> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(LiveShopCart::getMemberId,liveShopCartDTO.getMemberId())
                .eq(LiveShopCart::getDelFlag,"0")
                .orderByDesc(LiveShopCart::getCreateTime);
        IPage<LiveShopCart> shopCartPage = baseMapper.selectPage(page, wrapper);
        if(CollectionUtils.isEmpty(shopCartPage.getRecords())){
            return new Page<>();
        }
        List<Long> productIds = shopCartPage.getRecords().stream().map(LiveShopCart::getProductId).collect(Collectors.toList());

        Map<Long, Product> productMap = this.productService.getProductByIds(productIds).stream().collect(Collectors.toMap(Product::getProductId, p -> p));

        return shopCartPage.convert(item ->{
            Product product = productMap.get(item.getProductId());
            LiveShopCartVO liveShopCartVO = BeanUtils.copyBeanProp(product, LiveShopCartVO.class);
            liveShopCartVO.setId(item.getId());
            liveShopCartVO.setMemberId(item.getMemberId());
            liveShopCartVO.setQuantity(item.getQuantity());
            return liveShopCartVO;
        });
    }

    /**
     * 添加购物车
     * @param liveShopCartDTO
     * @author:kadorlin
     * @date:2022/10/25 09:11
     * @return:void
     **/
    @Override
    public void addItem(LiveShopCartDTO liveShopCartDTO) {
        QueryWrapper<LiveShopCart> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(LiveShopCart::getDelFlag,0)
                .eq(LiveShopCart::getMemberId,liveShopCartDTO.getMemberId())
                .eq(LiveShopCart::getProductId,liveShopCartDTO.getProductId());
        LiveShopCart shopCart = this.getOne(qw);
        if(Objects.nonNull(shopCart)){
            shopCart.setQuantity(shopCart.getQuantity()+liveShopCartDTO.getQuantity());
            this.updateById(shopCart);
            return;
        }
        LiveShopCart liveShopCart = BeanUtils.copyBeanProp(liveShopCartDTO, LiveShopCart.class);
        this.save(liveShopCart);
    }

    /**
     * 批量删除购物车
     * @param ids
     * @author:kadorlin
     * @date:2022/10/25 09:12
     * @return:void
     **/
    @Override
    public void batchRemove(List<Long> ids) {
        LambdaUpdateWrapper<LiveShopCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LiveShopCart::getDelFlag, 1)
                .in(LiveShopCart::getId, ids);
        update(updateWrapper);
    }

    /**
     * 修改数量
     * @param liveShopCartNum
     * @author:kadorlin
     * @date:2022/10/25 09:33
     * @return:void
     **/
    @Override
    public void edit(LiveShopCartNumDTO liveShopCartNum) {
        LambdaUpdateWrapper<LiveShopCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LiveShopCart::getQuantity, liveShopCartNum.getQuantity())
                .eq(LiveShopCart::getId, liveShopCartNum.getId());
        update(updateWrapper);
    }

}
